package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyBuilding;

@Service
public class WyBuildingService extends BaseService<WyBuilding, String> {

	
	/**
	 * 获取菜单树数据
	 * @param gardenid
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsTreeNode> getUITreeData(String gardenid, LoginUser loginUser, boolean isParent){
		List<JsTreeNode> list = new ArrayList<>();

		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.wuye.entity.WyBuilding(b.buildingid, b.buildingName, b.wyGarden.gardenid)");
		hql.append(" from WyBuilding b where b.wyGarden.gardenid=? ");
		if(!SystemCfg.isDataAdminUser(loginUser)){
			hql.append(" and b.buildingid in (select distinct v.wyBuilding.buildingid from SysUserBuildingRel v where v.sysUser.userid='"+loginUser.getUserid()+"')");
		}
		hql.append(" order by b.buildingName asc");
		
		List<WyBuilding> buildings = listByHql(hql.toString(), gardenid);
		if(buildings != null){
			for(WyBuilding building : buildings){
				list.add(new JsTreeNode(building.getBuildingid(), building.getBuildingName(), SystemCfg.UI_DATATYPE_BUILDING, null, isParent));
			}
		}
		return list;
	}
	
	
	/**
	 * 获取列表数据
	 * @param companyid
	 * @param gardenid
	 * @param buildingName
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String companyid, String gardenid, String buildingName, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.entity.WyBuilding(" +
				"b.buildingid, b.buildingCode, b.buildingName, b.address, b.buildingType," +
				" b.buildingStruct, b.buildingOrientation, b.area, b.remark, g.gardenName)" +
				" from WyBuilding b, WyGarden g" +
				" where b.wyGarden.gardenid=g.gardenid ");
		
		if(StringUtils.isNotEmpty(companyid) && !"0".equals(companyid)){
			querier.append(" and g.wyPropertyCompany.companyid=:companyid ");
			querier.setParam("companyid", companyid);
		}
		
		if(StringUtils.isNotEmpty(gardenid) && !"0".equals(gardenid)){
			querier.append(" and b.wyGarden.gardenid=:gardenid ");
			querier.setParam("gardenid", gardenid);
		}
		
		if(!SystemCfg.isDataAdminUser(loginUser)){
			querier.append(" and b.buildingid in (select distinct v.wyBuilding.buildingid from SysUserBuildingRel v where v.sysUser.userid=:userid)");
			querier.setParam("userid", loginUser.getUserid());
		}
		if(StringUtils.isNotEmpty(buildingName)){
			buildingName = buildingName.replace("'", "");
			querier.append(" and b.buildingName like :buildingName ");
			querier.setParam("buildingName", "%"+buildingName+"%");
		}
		
		querier.append(pageParam.getOrderby(" order by b.createTime desc"));
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	
	/**
	 * 添加信息
	 * @param building
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(WyBuilding building, LoginUser loginUser, HttpServletRequest request){
		
		Date now = new Date();
		building.setCreateTime(now);
		building.setUpdateTime(now);
		building.setCreateBy(loginUser.getLogin_name());
		building.setUpdateBy(loginUser.getLogin_name());
		
		this.save(building);
		
		return null;
		
	}
	
	
	/**
	 * 修改信息
	 * @param building
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(WyBuilding building, LoginUser loginUser, HttpServletRequest request){
		WyBuilding old = getSafeEntity(building.getBuildingid());
		if(old == null){
			return new SysError("参数错误");
		}
		
		String fields = "buildingCode,buildingName,address,buildingType,buildingStruct,buildingOrientation,area,remark";
		BeanUtil.setTargetFields(old, building, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<WyBuilding> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		return null;
		
	}
	
	
	/**
	 * 删除信息
	 * @param buildingids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] buildingids, LoginUser loginUser, HttpServletRequest request){
		for(String buildingid : buildingids){
			WyBuilding building = get(buildingid);
			if(building==null){
				continue;
			}
			
			Object obj = getByHql("select roomid from WyRoom where wyBuilding.buildingid=?", buildingid);
			if(obj!=null){
				return new SysError("楼宇 “"+building.getBuildingName()+"” 下有房间信息，不能删除.");
			}
			
			this.delete(building);
		}
		return null;
	}
	
	
	
}
